
export class guideDataCtrl {

    //记录在案的触发了但未完成的引导
    private _localGuides: number[];

    //已经完成了的引导
    private _completeGuides: GuideCache[];

    public get localGuides(): number[] {
        return this._localGuides;
    }

    public init() {
        this._localGuides = [];
        this._completeGuides = [];

        //TODO 从服务端获取引导数据更新_completeGuides,现在先从本地读
        this._completeGuides = JSON.parse(localStorage.getItem("completeGuides"))??[];

    }

    public setCompleteStep(guideId: number, stepId: number) {
        //TODO 同步至服务端

        //缓存至本地
        var cache = this.getCompleteCache(guideId);
        if (cache) {
            cache.StepId = stepId;
        } else {
            var newCache = new GuideCache();
            newCache.Id = guideId;
            newCache.StepId = stepId;
            this._completeGuides.push(newCache);
        }
        this.save();
    }

    public saveLocalGuide(id: number) {
        if (this._localGuides.indexOf(id) >= 0)
            return;

        this._localGuides.push(id);
        this.save();
    }

    public getCompleteStepId(guideId: number): number {
        var cache = this.getCompleteCache(guideId);
        return !!cache ? cache.StepId : -1;
    }

    public localGuideSave(id: number) {
        if (this._localGuides.indexOf(id) >= 0)
            return;

        this._localGuides.push(id);
        this.save();
    }

    public localGuideComplete(id: number) {
        var idx = this._localGuides.indexOf(id);
        if (idx < 0)
            return;

        this._localGuides.splice(idx, 1);
        this.save();
    }

    private getCompleteCache(guideId: number): GuideCache {
        return this._completeGuides.find(v => v.Id == guideId);
    }

    private save() {
        localStorage.setItem("localGuides", JSON.stringify(this._localGuides));
        localStorage.setItem("completeGuides", JSON.stringify(this._completeGuides));
    }
}

export class GuideCache {
    Id: number;
    StepId: number;
}
